package com.nstar.lrc.view
{
	import flash.utils.ByteArray;
	
	/*处理Sound类中的id3读取乱码问题*/
	public class EncodeUtf8
	{
		public function EncodeUtf8()
		{
		}
		
		//处理歌曲信息中的乱码
		public static function parseEncodeUtf8(str:String):String{
			if (str != null){
				var oriByteArr:ByteArray=new ByteArray();
				oriByteArr.writeUTFBytes(str);
				var needEncode:Boolean=false;
				for (var i=0; i<oriByteArr.length; i+= 2){
					if (oriByteArr[i] == 195 || oriByteArr[i] == 194){
						needEncode=true;
						break;
					}
					if (oriByteArr[i] == 32){
						i--;
					}
				}
				if (needEncode){
					var tempByteArr:ByteArray=new ByteArray;
					for (i=0; i<oriByteArr.length; i++){
						if (oriByteArr[i] == 194){
							tempByteArr.writeByte(oriByteArr[i + 1]);
							i++;
						} else if (oriByteArr[i] == 195){
							tempByteArr.writeByte(oriByteArr[i + 1] + 64);
							i++;
						} else{
							tempByteArr.writeByte(oriByteArr[i]);
						}
					}
					tempByteArr.position=0;
					return tempByteArr.readMultiByte(tempByteArr.bytesAvailable,"chinese");
				} else{
					return str;
				}
			} else{
				return "";
			}
		}
	}
}